package com.fengjing.launcher.parse;

import com.hg.library.config.FParser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

/**
 * Created by admin on 2017/9/22.
 */

public class AdParser {

    public static final String DEFAULT_NAME = "不知道";

    public static final String NAME = "name";
    public static final String YEAR = "year";
    public static final String DURATION = "duration";
    public static final String DIRECTOR = "director";
    public static final String ACTORS = "actors";
    public static final String DESCRIPTION = "description";

    public static final String getBannerVideoPath() {
        File file = new File(FParser.BASE_PATH + "ads/boot");
        File[] files = file.listFiles();
        File file1 = files[new Random().nextInt(files.length)];
        return file1.getPath();
    }

    public static final String getFeiChangPath() {
        return FParser.BASE_PATH + "videos/fcyy";
    }

    public static String switchName(String name) {
        switch (name) {
            case "kk3D":
                return "看看3D";
            case "kkdp":
                return "看看新片";
            case "kkjj":
                return "看看剧集";
            case "kkzy":
                return "看纪录片";
            default:
                return DEFAULT_NAME;
        }
    }

    /**
     * @param filePath
     * @param key      {$link {@link #NAME}}{$link {@link #YEAR}}{$link {@link #DURATION}}{$link {@link #DIRECTOR}}{$link {@link #ACTORS}}{$link {@link #DESCRIPTION}}
     * @return
     */

    public static String getDescriptionAttr(String filePath, String key) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"GB2312"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                int length = line.length();
                for (int i = 0; i < length; i++) {
                    if (line.charAt(i) == ':') {
                        if (line.substring(0, i) != "" && line.substring(0, i).equals(key)) {
                            return line.substring(i + 1, length);
                        }
                    }
                }
            }
            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AdParser.DEFAULT_NAME;
    }
}
